const sqlite3 = require("sqlite3-offline-next").verbose();
const db = new sqlite3.Database("./dev.db");

const schema = `
create table if not exists lorem (
  id integer primary key autoincrement,
  info varchar(25),
  created_at timestamp default current_timestamp,
  active boolean default 1
)`;

db.serialize(function () {
  db.run(schema);

  var stmt = db.prepare("INSERT INTO lorem (info) VALUES (?)");
  for (var i = 0; i < 10; i++) {
    stmt.run("Ipsum " + i);
  }
  stmt.finalize();

  db.each("SELECT rowid AS id, info FROM lorem", function (err, row) {
    console.log(row.id + ": " + row.info);
  });
});

// get method
db.get("select * from lorem where id = ?", [1], (err, row) => {
  if (err) return;
  console.log("get :", row.id, row.info, row.created_at, row.active);
});
// run
db.run("delete from lorem where id = -1", (err) => {
  console.log("delete error:", err);
});
//
db.all("select * from lorem", (err, rows) => {
  if (err) return;
  rows.forEach((row) => {
    console.log("get :", row.id, row.info, row.created_at, row.active);
  });
});
//
db.run("INSERT INTO lorem (info) VALUES (?)", ["man"], (err) => {
  if (err) return;
  db.get("select last_insert_rowid() as id", (err, row) => {
    if (err) return;
    console.log(row);
  });
});

db.close();
